package com.ztl.mymqtt;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.ztl.mymqtt.mqtt.MyMqttParas;
import com.ztl.mymqtt.mqtt.MyMqttParasManager;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;

public class SettingsActivity extends AppCompatActivity {

    private EditText etServerIp, etPort, etClientId, etUserName, etPassword,etTopicSub;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);

        etServerIp = findViewById(R.id.et_server_ip);
        etPort = findViewById(R.id.et_server_port);
        etClientId = findViewById(R.id.et_client_id);
        etUserName = findViewById(R.id.et_user_name);
        etPassword = findViewById(R.id.et_password);
        etTopicSub=findViewById(R.id.et_topic);




        MyMqttParas mqttParas= MyMqttParasManager.readConfig(this);

        etServerIp.setText(mqttParas.serverIp);
        etPort.setText(mqttParas.port);
        etClientId.setText(mqttParas.clientId);
        etUserName.setText(mqttParas.userName);
        etPassword.setText(mqttParas.passWord);
        etTopicSub.setText(mqttParas.topicSub);

        findViewById(R.id.save_settings).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MyMqttParasManager.saveConfig(SettingsActivity.this,new MyMqttParas(etServerIp.getText().toString()
                        , etPort.getText().toString()
                        , etClientId.getText().toString()
                        , etUserName.getText().toString()
                        , etPassword.getText().toString()
                        ,etTopicSub.getText().toString()));

                Toast.makeText(SettingsActivity.this,"保存成功！", Toast.LENGTH_LONG).show();
            }
        });


        findViewById(R.id.btBack).setOnClickListener(view -> {
            Intent intent=new Intent(SettingsActivity.this,MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        });

    }


}